﻿///***************************************************************
/// 시  스  템 : Updms.Prj
/// 단위시스템 : PRJ
/// 계      층 : Presentation Layer
/// 프로그램명 : Prj2011r
/// 파  일  명 : Prj2011r.aspx.cs
/// 설      명 : 기간별발생챠트(접수일기준)
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-12-23 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;

using Base.Dac;
using Updms.Prj.Exe;


namespace Updms.Prj
{
	public partial class Prj2011r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Prj2011r";
		public static readonly string gs_path_info = "/updms/Prj/" + PID + ".aspx";

		Global global = new Global();

		//검색
		protected string kw1 = "";	// 프로젝트아이디
		protected string kw2 = "";	// 기준년월 ~부터
		protected string kw3 = "";	// 기준년월 ~까지

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetProjectId();
			SetYearMonth();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetInit, SetSearchParameter, ShowKeywordLabel

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = ddl_search_prj_id.SelectedValue;
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						ddl_search_prj_id.SelectedValue = kw1;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						kw2 = ddl_search_from_year.SelectedValue + "-" + ddl_search_from_month.SelectedValue;
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						ddl_search_from_year.SelectedValue = kw2.Substring(0, 4).ToString();
						ddl_search_from_month.SelectedValue = kw2.Substring(5, 2).ToString();
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))	// url에 값이 없으면
					{
						kw3 = ddl_search_to_year.SelectedValue + "-" + ddl_search_to_month.SelectedValue;
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						ddl_search_to_year.SelectedValue = kw3.Substring(0, 4).ToString();
						ddl_search_to_month.SelectedValue = kw3.Substring(5, 2).ToString();
					}

					break;
				case "SEARCH":
					kw1 = ddl_search_prj_id.SelectedValue;
					kw2 = ddl_search_from_year.SelectedValue + "-" + ddl_search_from_month.SelectedValue;
					kw3 = ddl_search_to_year.SelectedValue + "-" + ddl_search_to_month.SelectedValue;
					break;
			}
		}


		#endregion

		#region 마스터 Method (SetProjectId, SetYearMonth)

		//프로젝트아이디
		private void SetProjectId()
		{
			DataSet lo_ds = new DataSet();
			Prj1010m_NT _Prj1010m_NT = new Prj1010m_NT();

			lo_ds.Merge(_Prj1010m_NT.GetProjectInfo());

			ddl_search_prj_id.Items.Clear();
			ddl_search_prj_id.DataSource = lo_ds;
			ddl_search_prj_id.DataMember = "UPDMS_PRJ_SUMMARY";
			ddl_search_prj_id.DataTextField = "Subject";
			ddl_search_prj_id.DataValueField = "Prj_Id";
			ddl_search_prj_id.DataBind();

			// 기본적으로 선택될 프로젝트 아이디
			ddl_search_prj_id.SelectedValue = DataSetUtil.GetFldStr(lo_ds, "UPDMS_PRJ_SUMMARY", "Sel_Prj_Id");
		}

		//검색조건의 년월설정
		private void SetYearMonth()
		{
			ddl_search_from_year.Items.Clear();
			ddl_search_to_year.Items.Clear();

			//프로젝트아이디에서 조회년월을 가져온다
			string ls_from_year = ddl_search_prj_id.SelectedItem.Text.Substring(1, 4);
			string ls_from_month = ddl_search_prj_id.SelectedItem.Text.Substring(6, 2);
			string ls_to_year = ddl_search_prj_id.SelectedItem.Text.Substring(14, 4);
			string ls_to_month = ddl_search_prj_id.SelectedItem.Text.Substring(19, 2);

			//등록된 년도만 조회 가능하도록
			int li_from_year = Convert.ToInt32(ls_from_year);
			int li_to_year = Convert.ToInt32(ls_to_year);
			for (int i = li_from_year; i <= li_to_year; i++)
			{
				string ls_year = i.ToString();
				ddl_search_from_year.Items.Add(new ListItem(ls_year, ls_year));
				ddl_search_to_year.Items.Add(new ListItem(ls_year, ls_year));
			}

			ddl_search_from_year.SelectedValue = ls_from_year; //프로젝트 시작년도
			ddl_search_from_month.SelectedValue = ls_from_month; //프로젝트 시작월
			ddl_search_to_year.SelectedValue = ls_to_year; //프로젝트 종료년도
			ddl_search_to_month.SelectedValue = ls_to_month; //프로젝트 종료월
		}

		#endregion

		# region 챠트 (DrawWeeklyCountBaseOnReceiveDt, DrawMonthlyCountBaseOnReceiveDt, SearchProcess)

		//기간별발생챠트(주별, 접수일기준)
		private void DrawWeeklyCountBaseOnReceiveDt(DataPack lo_dp)
		{
			int li_count = 0;
			DataSet lo_ds = new DataSet();

			//초기화
			chart_line_weekly.Data.Clear();			

			using (Prj2011r_NT _Prj2011r_NT = new Prj2011r_NT())
			{
				lo_ds.Merge(_Prj2011r_NT.GetWeeklyCountBaseOnReceiveDt(lo_dp));
				li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_PRJ_REPORT");
			}

			for (int i = 0; i < li_count; i++)
			{
				//수동으로 데이터 바인딩
				chart_line_weekly.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_PRJ_REPORT", i, "Cnt");

				//X축 라벨링
				chart_line_weekly.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, "UPDMS_PRJ_REPORT", i, "Week_Nm");

				//0은 안보여준다
				chart_line_weekly.Points[0, i].PointLabels.Visible = (chart_line_weekly.Data[0, i] > 0);
			}
		}

		//기간별발생챠트(월별, 접수일기준)
		private void DrawMonthlyCountBaseOnReceiveDt(DataPack lo_dp)
		{
			int li_count = 0;
			DataSet lo_ds = new DataSet();

			//초기화
			chart_line_monthly.Data.Clear();

			using (Prj2011r_NT _Prj2011r_NT = new Prj2011r_NT())
			{
				lo_ds.Merge(_Prj2011r_NT.GetMonthlyCountBaseOnReceiveDt(lo_dp));
				li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_PRJ_REPORT");
			}

			for (int i = 0; i < li_count; i++)
			{
				//수동으로 데이터 바인딩
				chart_line_monthly.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_PRJ_REPORT", i, "Cnt");

				//X축 라벨링
				chart_line_monthly.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, "UPDMS_PRJ_REPORT", i, "Month_Nm");

				//0은 안보여준다
				chart_line_monthly.Points[0, i].PointLabels.Visible = (chart_line_monthly.Data[0, i] > 0);
			}
		}

		//검색
		private void SearchProcess()
		{
			DataPack lo_dp = new DataPack();
			
			gs_page_mode = "R";
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			//검색
			lo_dp.AddProperty("ls_prj_id", typeof(string), kw1);
			lo_dp.AddProperty("ls_base_dt_from", typeof(string), kw2);
			lo_dp.AddProperty("ls_base_dt_to", typeof(string), kw3);

			DrawWeeklyCountBaseOnReceiveDt(lo_dp);
			DrawMonthlyCountBaseOnReceiveDt(lo_dp);
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":	// 처음 페이지로드시
					table_chart.Visible = false;
					break;
				case "R":	// 검색버튼 클릭시
					table_chart.Visible = true;
					break;
			}
		}

		#endregion

		#region 버튼이벤트

		//챠트
		protected void ibtn_chart_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}

		//프로젝트코드 변경
		protected void ddl_search_prj_id_SelectedIndexChanged(object sender, EventArgs e)
		{
			SetYearMonth();
		}

		#endregion

	}
}
